package de.gwtprime.server.model;

import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import de.gwtprime.server.sql.helper.HibernateHelper;
import de.gwtprime.shared.TransactionType;

/**
 * Deposits / Withdrawls
 * @author jan
 *
 */
@Entity
@Table(name = "transaction")
public class Transaction extends EntityBase {

	/**
	 * SerialVersionUID
	 */
	private static final long serialVersionUID = 1L;
	
	private TransactionType type;
	
	private String address;
	
	private Double amount;
	
	private String transactionId;
	
	private Date time;
	
	private Date timeReceived;
	
	private String blockHash;
	
	private Integer blockIndex;
	
	private Date blockTime;
	
	private User2 user;
	
	/**
	 * A transaction is considered as confirmed, if it has more than
	 * a defined number of confirmations
	 */
	private boolean confirmed;

	public TransactionType getType() {
		return type;
	}

	public void setType(TransactionType type) {
		this.type = type;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Double getAmount() {
		return amount;
	}

	public void setAmount(Double amount) {
		this.amount = amount;
	}

	public String getTransactionId() {
		return transactionId;
	}

	public void setTransactionId(String transactionId) {
		this.transactionId = transactionId;
	}

	public Date getTime() {
		return time;
	}

	public void setTime(Date time) {
		this.time = time;
	}

	public Date getTimeReceived() {
		return timeReceived;
	}

	public void setTimeReceived(Date timeReceived) {
		this.timeReceived = timeReceived;
	}

	public String getBlockHash() {
		return blockHash;
	}

	public void setBlockHash(String blockHash) {
		this.blockHash = blockHash;
	}

	public Integer getBlockIndex() {
		return blockIndex;
	}

	public void setBlockIndex(Integer blockIndex) {
		this.blockIndex = blockIndex;
	}

	public Date getBlockTime() {
		return blockTime;
	}

	public void setBlockTime(Date blockTime) {
		this.blockTime = blockTime;
	}

	@ManyToOne
	@JoinColumn
	public User2 getUser() {
		return user;
	}

	public void setUser(User2 user) {
		this.user = user;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result
				+ ((transactionId == null) ? 0 : transactionId.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		Transaction other = (Transaction) obj;
		if (transactionId == null) {
			if (other.transactionId != null)
				return false;
		} else if (!transactionId.equals(other.transactionId))
			return false;
		return true;
	}

	public boolean isConfirmed() {
		return confirmed;
	}

	public void setConfirmed(boolean confirmed) {
		this.confirmed = confirmed;
	}
	
	public static List<Transaction> loadUnconfirmedTransactions(){
		
		return HibernateHelper.queryDBForList(new Transaction(), 
				String.format("from %s where confirmed=0 and type=0", Transaction.class.getSimpleName()));
		
	}
}
